import time
from selenium.webdriver.common.by import By
from common.base import Base
from common.functions import open_browser
from selenium.webdriver.common.keys import Keys

class ShoppingCart(Base):
# 输入用户名
    def username(self, values,username):
        return self.send_keys(values, username)
# 输入密码
    def password(self, values,password):
        return self.send_keys(values, password)
# 点登陆
    def btn_click(self,values):
        return self.click(values)
# 点退出
    def login_out(self,values):
        self.click(values)
# 点首页
    def page(self,values):
        self.driver.maximize_window()
        self.click(values)
# 点商品
    def goods(self,values):
        self.click(values)
        time.sleep(1)
# 点立即购买(添加)
    def buy(self):
        return self.click((By.CLASS_NAME,'td1'))
# 点清空购物车
    def deleteall(self):
        return self.click((By.XPATH, '//*[@type="button"]'))
# 点删除商品
    def delete(self):
        self.click((By.LINK_TEXT,'删除'))
        alter = self.driver.switch_to.alert
        alter.accept()
# 查看购物车
    def check(self):
        self.click((By.XPATH, '//a[@title="查看购物车"]'))
# 更改商品页面里购买的商品数量
    def change_gnum(self,value,num):
        self.send_keys((value),Keys.BACK_SPACE)
        self.click((By.ID, 'number'))
        self.send_keys((By.ID, 'number'),num)
# 点继续购物(添加)
    def buy_again(self):
        self.click((By.XPATH, '//*[contains(@src,"themes/default/images/continue.gif")]'))
# 编辑购物车里的商品数量
    def change_cnum(self,values,num):
        self.clear(values)
        self.send_keys(values,num)
        self.click((By.XPATH, '//input[@value="更新购物车"]'))
        time.sleep(2)



if __name__ == '__main__':
    browser = open_browser()
    shopping = ShoppingCart(browser)
# 登陆界面
    shopping.get('http://139.129.26.163/user.php')
    shopping.username((By.NAME, 'username'),'username1')
    shopping.password((By.NAME, 'password'),'password')
# 登陆
    shopping.btn_click((By.NAME, 'submit'))
# 首页
    shopping.page((By.CLASS_NAME,'cur'))
# 点商品
    shopping.goods((By.XPATH, '//*[contains(@src,"images/201605/thumb_img/72_thumb_G_1462956048008.jpg")]'))
# 点立即购买
    shopping.buy()
# 点购物车查看
    shopping.check()
    time.sleep(2)
# 继续购物
    shopping.buy_again()
# 点商品
    shopping.goods((By.XPATH,'//*[contains(@src,"images/201605/thumb_img/70_thumb_G_1462955414561.jpg")]'))
# 在商品页面更改商品数量为13购买
    shopping.change_gnum((By.ID, 'number'),'13')
# 点立即购买
    shopping.buy()
# 在购物车更改商品数量
    shopping.change_cnum((By.XPATH, '/html/body/div[6]/div[1]/form/table[1]/tbody/tr[2]/td[5]/input'),'10')
# 点击删除
    shopping.delete()
    time.sleep(2)
# 点清空
    shopping.deleteall()
    shopping.quit()
# 点退出
    shopping.login_out((By.LINK_TEXT, '退出'))

